package app

import com.qc.CriticalMetrics
import com.qc.EmailReferrers

class AdminPanelController {
    static layout = 'admin'
    static defaultAction = 'emailList'

    def addAdmin() {}
    def removeAdmin() {}
    def adminList(){}

    def metricsConfigs() {}
    def saveMetricsConfigs() {}

    def configsList = {
           [list: CriticalMetrics.list()]
    }
    def saveConfigsList = {

    }

    def saveConfig = {
        def m = CriticalMetrics.get(params.id)
        m.value = params.value  as Integer
        m.avgValMultiplicand = params.avgValMultiplicand as Float
        if (m.save(flush:  true)) {
            flash.saved = true

        } else {
            flash.notSaved = true
            log.error(m.errors)
        }
        [].flatten()
        redirect(action: configsList)
    }

    def emailList = {
        if (params.id) {
            removeRlist()
        } else if (params.email) {
            removeEmail()
        }
        params.remove('id')
        [list: EmailReferrers.list()]
    }

    def removeEmail() {
        new EmailReferrers(email: params.email).save(flush: true)
        params.remove('email')
    }

    def removeRlist() {
        EmailReferrers.get(params.id).delete(flush: true)
        params.remove('id')
    }
}
